<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
require('./classes/control/control_product_mobile.php');

class ProductOperation
 {
	var $feature_c;
	function __construct(){
		
		$this->product_operation_c = new ProductOperationControl();
	}
    function loop_change_feature($length ,$_POST,$prefix = '')
    {
        $insert = '';
       //echo $length;
       //print_r($_POST);
       
        for($j=1;$j<=$length;$j++)
        {
         if(!empty($_POST['title_'.$prefix.'feature'.$j]))	
         {	
       	  if(!empty($_POST[$prefix.'feature_id'.$j]))
       	  {
       	  	if(empty($prefix))
       	  	{
       	  		
       	  	 $change_rows = $this->product_operation_c->update_feature_info('feature',(int)$_POST['feature_id'.$j],$_POST['description_feature'.$j],
       	     $_POST['title_feature'.$j],'',$_POST['image_min_feature'.$j],$_POST['image_max_feature'.$j]);
       	     
       	     
       	  	}else
       	  	{
       	  	
       	     $change_rows = $this->product_operation_c->update_feature_info($prefix.'feature',(int)$_POST[$prefix.'feature_id'.$j],$_POST['description_'.$prefix.'feature'.$j],
       	     $_POST['title_'.$prefix.'feature'.$j],$_POST['image_'.$prefix.'feature'.$j],'','');
       	  	}
       	  }else 
       	  {
       	   if(!empty($prefix))
       	   {
       	   	
       	   $insert.= "('".(int)$_POST['product_id']."','".$_POST['title_'.$prefix.'feature'.$j]."','".$_POST['image_'.$prefix.'feature'.$j]."',
   		   '".$_POST['description_'.$prefix.'feature'.$j]."','1','$j','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
       	   }else 
       	   {
       	   $insert.= "('".(int)$_POST['product_id']."','".$_POST['title_feature'.$j]."','".$_POST['image_min_feature'.$j]."','".$_POST['image_max_feature'.$j]."',
   		   '".$_POST['description_feature'.$j]."','0','$j','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
       	   }	
       	  }
         }else 
         {
          if(!empty($_POST[$prefix.'feature_id'.$j]))
       	  {
       	  $change_rows = $this->product_operation_c->update_feature_info('delete',(int)$_POST[$prefix.'feature_id'.$j]);
       	   }
         }
        }
        return  array('insert'=>$insert,'rows'=>$change_rows);
     }
    //公共产品修改增加
	function index()
	{
	 $array_tag = Array (0=> Array ( tag_name => 'New' ,tag_id => 'New' ), 1=> Array ( tag_name => 'Free' , tag_id => 'Free' ),
	 2=> Array ( tag_name => 'Upgrade' , tag_id => 'Upgrade' ),3=> Array ( tag_name => 'Top Seller' , tag_id => 'Top Seller' ),
	 4=> Array ( tag_name => 'Top Downloader' , tag_id => 'Top Downloader' )) ;	
	 if($_REQUEST['type'])
	 {	
	   $type = $_REQUEST['type']=='add'?'add':'update';
	   $insert = $insert_more = '';	
       if($_POST['sub_from'])
       {
       	 /*标签*/
       	 if(!empty($_POST['tag']))
	 	 {
	       $tag =  implode(',',$_POST['tag']);
	       $_POST['tag'] = $tag ;
	 	 }
       	 $change_rows  = 0;
	     $product_id = $this->product_operation_c->change_product($_POST);
	     if($product_id == -1 && $type == 'add')
	     {
	     	echo  '<script type="text/javascript"> alert("数据库已经存在此条记录！"); </script>';
	     }
	     elseif ($product_id == 0 && $type == 'add')
	     {
	     	echo  '<script type="text/javascript"> alert("增加失败！"); </script>';
	     }
         elseif ($product_id == 0 && $type == 'update')
	     {
	     	echo  '<script type="text/javascript"> alert("修改失败！"); </script>';
	     }elseif($product_id >0)
	     {
		     if($type == 'add')
	      	 {	
		      	if(!empty($product_id))
		      	{
		      	 for($i=1;$i<=$_POST['feature_start'];$i++)
		      	 {
		      	 	if(!empty($_POST['title_feature'.$i]))
		      	 	{
		      	 	$insert.= "('".$product_id."','".$_POST['title_feature'.$i]."','".$_POST['image_min_feature'.$i]."','".$_POST['image_max_feature'.$i]."',
			   		'".$_POST['description_feature'.$i]."','0','$i','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
		      	 	}
		      	 }
		      	 for($j=1;$j<=$_POST['more_feature_start'];$j++)
		      	 {
		      	 	if(!empty($_POST['title_more_feature'.$j]))
		      	 	{
		      	 	$insert_more.= "('".$product_id."','".$_POST['title_more_feature'.$j]."','".$_POST['image_more_feature'.$j]."',
			   		'".$_POST['description_more_feature'.$j]."','1','$j','".$_SERVER['REQUEST_TIME']."','".$_SESSION['AName']."'),";
		      	 	}
		      	 }
		      	 if(!empty($product_id) && empty($insert) && empty($insert_more))
		      	 {
		      	 	$change_rows = 1;
		      	 }
		      	 
		      	}
	      	}elseif($type == 'update')
	      	{
	      	    $feature_a = $more_feature_a = array();
	      	    $feature_a = 	$this->loop_change_feature($_POST['feature_start'] ,$_POST);
	      	   
	      	    $insert = $feature_a['insert'];
	      	    $change_rows = $feature_a['rows'];
	      	    $more_feature_a = $this->loop_change_feature($_POST['more_feature_start'] ,$_POST,'more_');
	      	    $insert_more = $more_feature_a['insert'];
	      	    $change_rows = $change_rows + $more_feature_a['rows'];
	      	    if(!empty($_POST['product_id']) && empty($insert) && empty($insert_more))
		        {
		      	  $change_rows = 1;
		        }
	      	}
	   }
       if(!empty($insert))
	   {
		   $insert = substr($insert, 0, strlen($insert) - 1) ;
		   //echo "pppooopppo";
		   //echo $insert;die();
		   $change_rows = $this->product_operation_c->insert_feature_info($insert,$product_id,'insert');//批量插入
	   }
	   if(!empty($insert_more))
	   {
		   $insert_more = substr($insert_more, 0, strlen($insert_more) - 1) ;
		   $change_rows = $this->product_operation_c->insert_feature_info($insert_more,$product_id,'m_insert');//批量插入
	   }
	   
	   if($type == 'add')
	   {
	      if($change_rows>0)
	      {
		    	echo  '<script type="text/javascript"> alert("增加成功！"); 
		    	window.location.href="/wondershare/product_m_list.php?action=index"; </script>';
	      }
	   }elseif($type == 'update')
	   {
          if($change_rows>0)
		  {
		    	echo  '<script type="text/javascript"> alert("修改成功！");
	 		    window.location.href="/wondershare/product_m_list.php?action=index"; </script>';
		  }
	   }
	   
      }
      if($type == 'add')
      {
       $GLOBALS['smarty']->assign('all_tag', $this->get_checkbox(array(0=>'0'),$array_tag,'tag'));
       $GLOBALS['smarty']->assign('feature',     array(0=>0));
       $GLOBALS['smarty']->assign('m_feature',   array(0=>0));
       $GLOBALS['smarty']->assign('c_feature',   1);
       $GLOBALS['smarty']->assign('c_m_feature', 1);
	   $GLOBALS['smarty']->assign('tile',     '添加');
	   $GLOBALS['smarty']->assign('form_act',     'add');
	   $GLOBALS['smarty']->assign('cat_select',   cat_list(0, 0, true,0,true,'   WHERE c.cat_web = 1 AND c.cat_type = 2  ',1));
	
      }elseif($type == 'update')
      {
       $product_info = $feature = $m_feature = array();
   	   if(!empty($_GET['product_id']))
   	   {
   	    $product_info = $this->product_operation_c->get_product_info('edit',0,(int)$_GET['product_id']);
   	    
        $feature = $this->product_operation_c->get_feature_info((int)$_GET['product_id'],'feature');
        $m_feature = $this->product_operation_c->get_feature_info((int)$_GET['product_id'],'m_feature');
        $cat_select = cat_list(0, $product_info[0]['cat_id'], true,0,true,'   WHERE c.cat_web = 1 AND c.cat_type = 2  ',1);
        $GLOBALS['smarty']->assign('all_tag', $this->get_checkbox(explode(',',$product_info[0]['tag']),$array_tag,'tag'));
        $GLOBALS['smarty']->assign('cat_select',$cat_select  );
        //print_r($feature);
        $GLOBALS['smarty']->assign('feature',     $feature);
        $GLOBALS['smarty']->assign('m_feature',   $m_feature);
        $GLOBALS['smarty']->assign('c_feature',   count($feature));
        $GLOBALS['smarty']->assign('c_m_feature', count($m_feature));
   	    $GLOBALS['smarty']->assign('product',     $product_info[0]);
   	   }
   	   $GLOBALS['smarty']->assign('tile',     '修改');
	   $GLOBALS['smarty']->assign('form_act',     'update');
	   unset($product_info);unset($feature);unset($m_feature);
      }
      $GLOBALS['smarty']->display('product_mobile_info.htm');  
    }
   
   }
  /**
  * 得到checkbox组合
  *
  * @param   string        $attributes_id      产品属性
  * @param   string        $all_attributes     属性表所有属性
  * @param   string        $type               类型
  * @return  string
  */
	function get_checkbox($select_arr_id,$all_checkbox,$type)
	{
	 $str = '';
	 if($type == 'attributes')
	 {
	   $checkbox_name = "attributes[]";
       $id = 'id';
       $name = 'name';
	 }elseif($type == 'bar')
	 {
	  $checkbox_name = "bar[]";
      $id = 'block_id';
      $name = 'block_name';
	 }elseif($type == 'tag')
	 {
	  $checkbox_name = "tag[]";
      $id = 'tag_id';
      $name = 'tag_name';
	 }
	 for($i=0;$i<count($all_checkbox);$i++)
     {
      $fage = 1;
      foreach ($select_arr_id as $key=>$value)
	  {
       
       
       if($value == $all_checkbox[$i][$id])
       {
       	
        $fage = 0;
        $str.='<input type="checkbox" name="'.$checkbox_name.'" value="'.$all_checkbox[$i][$id].'"  
        title = "'.htmlspecialchars($all_checkbox[$i][$name]).'" checked="checked"/>'.htmlspecialchars($all_checkbox[$i][$name]).' &nbsp;&nbsp;';
        }
       }
       if($fage == 1)
       {
       $str.='<input type="checkbox" name="'.$checkbox_name.'" value="'.$all_checkbox[$i][$id].'" 
       title = "'.htmlspecialchars($all_checkbox[$i][$name]).'" />'.htmlspecialchars($all_checkbox[$i][$name]).' &nbsp;&nbsp;';
       }
	  }
	  return $str;
	  	
	}
 }
$action    = $_REQUEST['action']; 
$product_operation   = new ProductOperation;
if(method_exists($product_operation, $action)) 
{
	$product_operation->$action();
}else{
	$product_operation->index();
}

	
?>
